package com.ruoyi.operator.controller;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.operator.domain.OptShopCategory;
import com.ruoyi.operator.domain.OptShopProduct;
import com.ruoyi.operator.service.IOptShopCategoryService;
import com.ruoyi.operator.service.IOptShopProductService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 商家商品Controller
 * 
 * @date 2021-02-08
 */
@Controller
@RequestMapping("/operator/product")
public class OptShopProductController extends BaseController
{
    private String prefix = "operator/product";

    @Autowired
    private IOptShopProductService optShopProductService;
    @Autowired
    private IOptShopCategoryService optShopCategoryService;

    @RequiresPermissions("operator:product:view")
    @GetMapping()
    public String product()
    {
        return prefix + "/product";
    }

    /**
     * 查询商家商品列表
     */
    @RequiresPermissions("operator:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OptShopProduct optShopProduct)
    {
        startPage();
        List<OptShopProduct> list = optShopProductService.selectOptShopProductList(optShopProduct);
        return getDataTable(list);
    }

    /**
     * 导出商家商品列表
     */
    @RequiresPermissions("operator:product:export")
    @Log(title = "商家商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OptShopProduct optShopProduct)
    {
        List<OptShopProduct> list = optShopProductService.selectOptShopProductList(optShopProduct);
        ExcelUtil<OptShopProduct> util = new ExcelUtil<OptShopProduct>(OptShopProduct.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 新增商家商品
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        OptShopCategory  optshopcategory = new OptShopCategory();
        SysUser currentUser  = (SysUser) SecurityUtils.getSubject().getPrincipal();
        optshopcategory.setDeptId(String.valueOf(currentUser.getDeptId()));
        List<OptShopCategory> list = optShopCategoryService.selectOptShopCategoryinfo(optshopcategory);
        mmap.put("OptShopProductinfo", list);
        return prefix + "/add";
    }

    /**
     * 新增保存商家商品
     */
    @RequiresPermissions("operator:product:add")
    @Log(title = "商家商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OptShopProduct optShopProduct) {
        return toAjax(optShopProductService.insertOptShopProduct(optShopProduct));
    }

    /**
     * 修改商家商品
     */
    @GetMapping("/edit/{productId}")
    public String edit(@PathVariable("productId") String productId, ModelMap mmap)
    {
        OptShopProduct optShopProduct = optShopProductService.selectOptShopProductById(productId);
        mmap.put("optShopProduct", optShopProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存商家商品
     */
    @RequiresPermissions("operator:product:edit")
    @Log(title = "商家商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OptShopProduct optShopProduct)
    {
        return toAjax(optShopProductService.updateOptShopProduct(optShopProduct));
    }

    /**
     * 删除商家商品
     */
    @RequiresPermissions("operator:product:remove")
    @Log(title = "商家商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(optShopProductService.deleteOptShopProductByIds(ids));
    }
}
